package com.island.controller.bill;

import com.alibaba.fastjson.JSON;
import com.island.pojo.Bill;
import com.island.pojo.Provider;
import com.island.service.bill.BillService;
import com.island.service.provider.ProviderService;
import com.island.utils.PageSupport;
import com.island.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jsp")
public class BillController {

    @Autowired
    private BillService billService;
    @Autowired
    private ProviderService providerService;
    @Autowired
    private PageSupport pageSupport;

    //1.获取bill页的数据
    @RequestMapping("/queryBill")
    public String queryBill(HttpServletRequest request){
        String pageIndex = request.getParameter("pageIndex");
        String productName = request.getParameter("queryProductName");
        String providerId = request.getParameter("queryProviderId");
        String isPayment = request.getParameter("queryIsPayment");

        Map<String,Object> map = new HashMap<String, Object>();
        //使查询ProviderMapper.xml的queryProviderList中的limit #{currentPageNo},#{pageSize}失效
        map.put("currentPageNo",-1);
        map.put("pageSize",0);
        //获取provider表的所有数据
        List<Provider> providerList=providerService.queryProviderList(map);

        if(productName!=null){
            map.put("productName",productName);
        }
        if(providerId!=null){
            //System.out.println(providerId);
            map.put("providerId",providerId);
        }
        if(isPayment!=null){
            map.put("isPayment",isPayment);
        }
        //根据productName、providerId或isPayment获取bill表中数据的总数
        int totalCount = billService.getBillCount(map);
        //根据当前页数和相应表中数据的总数通过PageUtil获取相应的pageSupport对象
        pageSupport= PageUtil.getPageSupport(pageIndex,totalCount);
        //设置下一页页数和每页最大数据条数
        map.put("currentPageNo",(pageSupport.getCurrentPageNo()-1)* pageSupport.getPageSize());
        map.put("pageSize",pageSupport.getPageSize());

        //获取bill列表
        List<Bill> billList = billService.queryBillList(map);
        //传相应数据到前端
        request.setAttribute("queryProductName",productName);
        request.setAttribute("queryProviderId",providerId);
        request.setAttribute("providerList",providerList);
        request.setAttribute("queryIsPayment",isPayment);
        request.setAttribute("billList",billList);
        request.setAttribute("pageSupport",pageSupport);
        return "billlist";
    }

    //2.删除bill数据
    @RequestMapping("/deletebill")
    @ResponseBody
    public String deleteBill(String billid){
        System.out.println(billid);
        HashMap<String,String> map = new HashMap<String, String>();
        int builId = Integer.parseInt(billid);
        //根据前端传入的billid获取相应bill对象
        Bill bill = billService.queryBillById(builId);
        if(bill!=null){
            try {
                billService.deleteBillById(builId);
                System.out.println("您成功删除了"+bill);
                map.put("delResult","true");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("您没能删除"+bill);
                map.put("delResult","false");
            }
        }else {
            System.out.println("该订单不存在");
            map.put("deResulte","notexist");
        }
        //返回json对象给ajax
        return JSON.toJSONString(map);

    }

    //3.获取相应billid的数据
    @RequestMapping("/viewbill")
    public String viewBill(String billid, Model model){

        int billId = Integer.parseInt(billid);
        //根据前端传入的billid获取相应bill对象
        Bill bill = billService.queryBillById(billId);
        model.addAttribute("bill",bill);
        return "billview";
    }

    //4.根据前端输入的数据添加到bill表中
    @RequestMapping("/addBill")
    public String addBill(Bill bill){
        //设置创建订单时间
        bill.setCreationDate(new Date());
        if(billService.addBill(bill)>0){
            System.out.println("添加成功！");
        }else {
            System.out.println("添加失败！");
        }

        //重定向到”/jsp/queryBill“
        return "redirect:/jsp/queryBill";
    }

    //5.跳转到billmodify页面
    @RequestMapping("/tomodifybill")
    public String tomodifyBill(String billid,Model model){
        int billId = Integer.parseInt(billid);
        Bill bill = billService.queryBillById(billId);
        //回显数据给前端
        model.addAttribute("bill",bill);
        return "billmodify";
    }

    //6.根据前端输入的数据更新相应的数据
    @RequestMapping("/modifybill")
    public String modifyBill(Bill bill){
        //设置修改bill表相应数据的时间
        bill.setModifyDate(new Date());
        try {
            billService.updateBill(bill);
            System.out.println("更新成功！");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("更新失败！");
        }

        //重定向到”/jsp/queryBill"
        return "redirect:/jsp/queryBill";
    }

    //7.获取billlist界面下拉框中provider.proName的全部数据
    @RequestMapping("/getProviderList")
    @ResponseBody
    public String getproviderlist(){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("currentPageNo",-1);
        map.put("pageSize",0);
        List<Provider> providers = providerService.queryProviderList(map);
        return JSON.toJSONString(providers);
    }
}
